<?php

namespace App\Http\Middleware;

use Closure;
use App\Services\User\UserIndex;

class CheckIdentity
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        # token
        //$requestToken  = !empty($_COOKIE['request_token']) ? $_COOKIE['request_token'] : null;
        $requestToken  = !empty(session('request_token')) ? session('request_token') : null;
        if (!session('userId') && $request->is('backstage/*')) {
            return redirect('backstage/login');
        } elseif (!$requestToken) {

        }
        # 判断前后台用户 之后检查是否登录了
        $user = UserIndex::Token()->hGetAll($requestToken);
        if ($request->is('backstage/*')) {
            if ($user['status'] != 1 || empty($user['res']['id']) ||  intval($user['res']['id']) > 1000)
                return redirect('backstage/login');
        } else {
            if ($user['status'] != 1 || empty($user['res']['id']) || intval($user['res']['id']) < 1000)
                return redirect('');
        }

        return $next($request);
    }
}
